package com.example.demoexample.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class MyLooperViewPager extends ViewPager {
    private OnLooperViewpagerListener mOnLooperViewpagerListener;

    public MyLooperViewPager(@NonNull Context context) {
        super(context);
    }

    public MyLooperViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                if (mOnLooperViewpagerListener != null){
                    mOnLooperViewpagerListener.onLooperTouch(true);
                }
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                if (mOnLooperViewpagerListener != null){
                    mOnLooperViewpagerListener.onLooperTouch(false);
                }
                break;
        }

        return super.onTouchEvent(ev);
    }

    public void setOnLooperViewpagerListener(OnLooperViewpagerListener onLooperViewpagerListener){
        this.mOnLooperViewpagerListener = onLooperViewpagerListener;
    }

    public interface OnLooperViewpagerListener{
        void onLooperTouch(boolean isTouch);
    }

}
